package com.uroad.jilinpda.util;

import android.content.Context;


import com.genvict.core.GvDevOptConstant;
import com.genvict.handset.GvHandsetInterface;
import com.uroad.lib.util.log.LogUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 将原ObuKeepAlive类中选择1001目录指令替换为读0016命令
 * 因为调接口前会获取随机数 接口返回写二进制文件指令
 * 中间如果切换目录会导致随机数失效 报6984
 * 详情见 https://blog.csdn.net/lyx_win/article/details/78684693
 */
public class ObuKeepAlive {
    private static ObuKeepAlive instance;
    private SdkHelper sdkHelper;
    private Timer mTimer = null;
    private int mDelayTime = 100;
    private Boolean mPause = false;
    private int mTotalTimes = 20000;
    private int mPassTime = 0;
    private String mObuProviderCode = "";
    private int taskStatus = 0;
//    private GvHandsetInterface obuInterface;
    private String[] command = new String[]{"00b0970037"};
    private int channelID = 1;
    private boolean isKeepAlive = false;//是否保活

    public ObuKeepAlive(GvHandsetInterface obuInterface) {
//        this.obuInterface = obuInterface;
    }

    private ObuKeepAlive() {
    }

    public void initSdkHelper(Context context) {
        sdkHelper = SdkHelper.getInstance(context);
    }

    /**
     * 获取保活类单例
     * @return
     */
    public static ObuKeepAlive getInstance() {
        if (instance == null) {
            synchronized (ObuKeepAlive.class) {
                if (instance == null) {
                    instance = new ObuKeepAlive();
                }
            }
        }
        return instance;
    }


    public void setDelayTime(int delayTime) {
        if (delayTime > 200) {
            this.mDelayTime = 200;
        } else if (delayTime < 50) {
            this.mDelayTime = 50;
        } else {
            this.mDelayTime = delayTime;
        }

    }

    //是否保活状态
    public boolean isKeepAlive() {
        return isKeepAlive;
    }

    /**
     * 设置通道号 操作OBU设置为1 操作卡是设置为2
     * @param channelID 1 ICC(ETC卡) 2 ESAM（OBU）
     */
    public void setChannelID(int channelID) {
        this.channelID = channelID;
    }

    public void start() {
        isKeepAlive = true;
        if (this.mTimer == null) {
            this.mTimer = new Timer();
        }

        LogUtil.i("ObuKeepAlive","start Timer");
        this.taskStatus = 0;
        this.mTimer.schedule(new ObuKeepAlive.ScheduleRunTask(), 0L, (long)this.mDelayTime);
    }

    public void stop() {
        isKeepAlive = false;
        if (this.mTimer != null) {
            this.mTimer.cancel();
            this.mTimer = null;
            LogUtil.i("ObuKeepAlive","stop cancel Timer");
        }

        this.mPause = false;
        this.mPassTime = 0;
    }

    public void pause() {
        this.mPause = true;
        this.mPassTime = 0;
    }

    public void resume() {
        this.mPause = false;
    }

    public void setObuPorviderCode(String obuProviderCode) {
        if (obuProviderCode != null && !obuProviderCode.equals("")) {
            try {
                this.mObuProviderCode = obuProviderCode.substring(0, 2);
            } catch (Exception var3) {
                var3.printStackTrace();
                this.mObuProviderCode = "";
            }
        } else {
            this.mObuProviderCode = "";
        }
    }

    public void setTaskStatus(int status) {
        this.taskStatus = status;
    }

    public int getTaskStatus() {
        return this.taskStatus;
    }

    private void runTask(int setMMiParam, int setMMiTimeoutMs) {
        if (this.taskStatus == 0) {
            LogUtil.i("OBUKeepAlive","to keep alive send cosCommand");
            this.command[0] = "00b0970037";
//            this.obuInterface.obuCosCommand(channelID, 1, this.command);
            try {
                sdkHelper.cosChannelAlive(GvDevOptConstant.CHANNEL_TYPE_OBU, channelID, 1, this.command);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            this.taskStatus = 2;
        }
    }

    class ScheduleRunTask extends TimerTask {
        ScheduleRunTask() {
        }

        public void run() {
            ObuKeepAlive.this.mPassTime = ObuKeepAlive.this.mPassTime + ObuKeepAlive.this.mDelayTime;
            if (ObuKeepAlive.this.mPassTime > ObuKeepAlive.this.mTotalTimes) {
                LogUtil.i("OBUKeepAlive","KeepAlive run too much time, so stop it.");
                if (ObuKeepAlive.this.mTimer != null) {
                    ObuKeepAlive.this.mTimer.cancel();
                    ObuKeepAlive.this.mTimer = null;
                    LogUtil.i("OBUKeepAlive","run too much time cancel Timer");
                }

                ObuKeepAlive.this.mPassTime = 0;
            } else if (!ObuKeepAlive.this.mPause) {
                int setMMIPara = 1;
                int timeout = 410;
                ObuKeepAlive.this.runTask(setMMIPara, timeout);
            }
        }
    }
}
